package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsBrand;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService{


    @Autowired
    private DtsBrandMapper brandMapper;
    @Autowired
    private DtsCategoryMapper categoryMapper;


















    @Override
    public IPage<DtsBrand> queryBrandPage(Integer id,
                                          String name,
                                          Integer page,
                                          Integer limit,
                                          String sort,
                                          String order) {
        //1. 创建分页对象
        //参数1: 当前页, 参数2: 每页显示条数


        IPage<DtsBrand> ipage = new Page<>(page,limit);
        //2. 封装查询对象
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        //判断查询条件不为空拼接查询条件

        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);

        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);

        }
        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            //判断排序方式是否为升序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);

            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }



        }//3查询并返回结果
        IPage<DtsBrand> resultPage = brandMapper.selectPage(ipage,queryWrapper);


        return resultPage;
    }

    @Override
    public DtsBrand add(DtsBrand brand)  {
        //判断制造商品牌id是否存在
        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getName,brand.getName());
        Integer count = brandMapper.selectCount(qw);
        //如果count大于0，说明此制造商id已存在
        if(count > 0) {
            return (DtsBrand) ResponseUtil.fail(-1, "此制造商ID已存在");
        }
        brand.setId(brand.getId());
        brand.setName(brand.getName());
        brand.setAdminId(brand.getAdminId());
        brand.setAddTime(new Date());
        brand.setFloorPrice(brand.getFloorPrice());







        brand.setDefaultCategoryId(brand.getDefaultCategoryId());


        brandMapper.insert(brand);

        return brand;
    }

    @Override
    public DtsBrand update(DtsBrand brand) {
        brand.setName(brand.getName());
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);


        return brand;
    }

    @Override
    public void deleted(DtsBrand brand) {
        brandMapper.deleteById(brand.getId());

    }
}
